package org.jeecg.modules.outOrder.service.impl;

import org.jeecg.modules.outOrder.entity.OutOrder;
import org.jeecg.modules.outOrder.entity.OutOrderZi;
import org.jeecg.modules.outOrder.mapper.OutOrderZiMapper;
import org.jeecg.modules.outOrder.mapper.OutOrderMapper;
import org.jeecg.modules.outOrder.service.IOutOrderService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 出库单
 * @Author: jeecg-boot
 * @Date:   2024-06-27
 * @Version: V1.0
 */
@Service
public class OutOrderServiceImpl extends ServiceImpl<OutOrderMapper, OutOrder> implements IOutOrderService {

	@Autowired
	private OutOrderMapper outOrderMapper;
	@Autowired
	private OutOrderZiMapper outOrderZiMapper;
	
	@Override
	@Transactional
	public void saveMain(OutOrder outOrder, List<OutOrderZi> outOrderZiList) {
		outOrderMapper.insert(outOrder);
		if(outOrderZiList!=null && outOrderZiList.size()>0) {
			for(OutOrderZi entity:outOrderZiList) {
				//外键设置
				entity.setId(null);
				entity.setOutOrderId(outOrder.getId());
				outOrderZiMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(OutOrder outOrder,List<OutOrderZi> outOrderZiList) {
		outOrderMapper.updateById(outOrder);
		
		//1.先删除子表数据
		outOrderZiMapper.deleteByMainId(outOrder.getId());
		
		//2.子表数据重新插入
		if(outOrderZiList!=null && outOrderZiList.size()>0) {
			for(OutOrderZi entity:outOrderZiList) {
				//外键设置
				entity.setOutOrderId(outOrder.getId());
				outOrderZiMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		outOrderZiMapper.deleteByMainId(id);
		outOrderMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			outOrderZiMapper.deleteByMainId(id.toString());
			outOrderMapper.deleteById(id);
		}
	}
	
}
